﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Zeus.Thunder.Core;

namespace SharpTrainer.Concurrency
{
    class AsyncCallbackTest : ITestCase
    {
        public void Run()
        {
            string url = "http://localhost:3650/TestHandler.ashx";
            
            WebRequest req = WebRequest.Create(url);
            AsyncCallback callback = delegate(IAsyncResult iar)
            {
                WebResponse resp = req.EndGetResponse(iar);

                // this is the mock callback logic from server
                ProcessResponse(resp);
            };

            req.BeginGetResponse(callback, null);
            Console.WriteLine("Requesting, please wait ... ");
        }

        private void ProcessResponse(WebResponse response)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string incReq = reader.ReadToEnd();

            Console.WriteLine();
            Console.WriteLine("Response: {0}", incReq);
            Console.WriteLine();
        }
    }
}
